package com.yxx.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.yxx.annotation.OptLogger;
import com.yxx.constant.OptTypeConstant;
import com.yxx.entityModel.vo.Result.PageResult;
import com.yxx.entityModel.vo.Result.ResponseResult;
import com.yxx.entityModel.vo.query.CarouselQuery;
import com.yxx.entityModel.vo.request.CarouselRequest;
import com.yxx.entityModel.vo.request.CarouselStatusRequest;
import com.yxx.entityModel.vo.response.CarouselBackResponse;
import com.yxx.entityModel.vo.response.CarouselResponse;
import com.yxx.service.CarouselService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * 轮播图控制器
 * @author yuxiangxun
 * @date 2025/2/1
 * @apiNote
 */
@RestController
@Slf4j
@Api(tags = "轮播图模块")
public class CarouselController {
    @Resource
    private CarouselService carouselService;

    /**
     * 查看轮播图列表
     * @return 轮播图列表
     */
    @ApiOperation(value = "查看轮播图列表")
    @GetMapping("/carousel/list")
    public ResponseResult<List<CarouselResponse>> getCarouselList() {
        return ResponseResult.success(carouselService.getCarouselList());
    }

    /**
     * 查看轮播图列表
     * @param carouselQuery 查询条件
     * @return  轮播图列表
     */
    @ApiOperation(value = "查看轮播图列表")
    @SaCheckPermission("web:carousel:list")
    @GetMapping("/admin/carousel/list")
    public ResponseResult<PageResult<CarouselBackResponse>> getCarouselVOList(CarouselQuery carouselQuery) {
        return ResponseResult.success(carouselService.getCarouselListVO(carouselQuery));
    }

    /**
     * 上传轮播图
     * @param file 文件
     * @return 图片地址
     */
    @OptLogger(value = OptTypeConstant.UPLOAD)
    @ApiOperation(value = "上传轮播图片")
    @ApiImplicitParam(name = "file", value = "轮播图片", required = true, dataType = "MultipartFile")
    @SaCheckPermission("web:carousel:upload")
    @PostMapping("/admin/carousel/upload")
    public ResponseResult<String> uploadCarousel(@RequestParam("file") MultipartFile file){
        return ResponseResult.success(carouselService.uploadCarousel(file));
    }

    /**
     * 添加轮播图
     * @param carouselRequest 轮播图信息
     * @return
     */
    @OptLogger(value = OptTypeConstant.ADD)
    @ApiOperation(value = "添加轮播图")
    @SaCheckPermission("web:carousel:add")
    @PostMapping("/admin/carousel/add")
    public ResponseResult<?> addCarousel(@Validated @RequestBody CarouselRequest carouselRequest) {
        carouselService.addCarousel(carouselRequest);
        return ResponseResult.success();
    }

    /**
     * 修改轮播图
     * @param carouselRequest 轮播图信息
     * @return {@link ResponseResult<>}
     */
    @OptLogger(value = OptTypeConstant.UPDATE)
    @ApiOperation(value = "修改轮播图")
    @SaCheckPermission("web:carousel:update")
    @PostMapping("/admin/carousel/update")
    public ResponseResult<?> updateCarousel(@Validated @RequestBody CarouselRequest carouselRequest) {
        carouselService.updateCarousel(carouselRequest);
        return ResponseResult.success();
    }

    /**
     * 删除轮播图
     *
     * @param carouselIdList 轮播图id集合
     * @return {@link ResponseResult<>}
     */
    @OptLogger(value = OptTypeConstant.DELETE)
    @ApiOperation(value = "删除轮播图")
    @SaCheckPermission("web:carousel:delete")
    @DeleteMapping("/admin/carousel/delete")
    public ResponseResult<?> deleteCarousel(@RequestBody List<Integer> carouselIdList) {
        carouselService.removeByIds(carouselIdList);
        return ResponseResult.success();
    }

    /**
     * 修改轮播图状态
     *
     * @param carouselStatusRequest 轮播图状态
     * @return {@link ResponseResult<>}
     */
    @OptLogger(value = OptTypeConstant.UPDATE)
    @ApiOperation(value = "修改轮播图状态")
    @SaCheckPermission("web:carousel:status")
    @PutMapping("/admin/carousel/status")
    public ResponseResult<?> updateCarouselStatus(@Validated @RequestBody CarouselStatusRequest carouselStatusRequest) {
        carouselService.updateCarouselStatus(carouselStatusRequest);
        return ResponseResult.success();
    }
}
